package holder;

import java.util.List;

import com.example.googlemarketdemo.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import conf.constants.URLS;

import utils.BitmapHepler;
import utils.UIUtils;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import base.BaseHolder;

/**
 * 轮播图的holder类
 * 
 * @author Anson-pc
 * 
 */


public class HomePictureHolder extends BaseHolder<List<String>> {

	@ViewInject(R.id.item_home_picture_pager)
	view.InnerViewPager mViewpager;
	@ViewInject(R.id.item_home_picture_container_indicator)
	LinearLayout mContainerIndicator;
	private List<String> mUrls;

	@Override
	protected View initHolderView() {
		View view = View.inflate(UIUtils.getContext(),
				R.layout.item_home_pictures, null);

		ViewUtils.inject(this, view);
		return view;
	}

	/**
	 * 刷新页面
	 */
	@Override
	protected void refreshHolderView(List<String> urls) {
		// 保存数据
		mUrls = urls;

		mViewpager.setAdapter(new PicturesAdapter());

		// 加入指示的点,通过for循环遍历的点
		for (int i = 0; i < urls.size(); i++) {

			ImageView ivIndicator = new ImageView(UIUtils.getContext());
			ivIndicator.setImageResource(R.drawable.indicator_normal);

			// 默认选中第一个
			if (i == 0) {
				ivIndicator.setImageResource(R.drawable.indicator_selected);
			}

			// 默认单位为px,因此要自定义转换单位的方法
			int width = UIUtils.dip2px(6);
			int height = UIUtils.dip2px(6);

			LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
					width, height);
			params.leftMargin = UIUtils.dip2px(6);
			params.bottomMargin = UIUtils.dip2px(6);
			mContainerIndicator.addView(ivIndicator, params);
		}
		// 设置ViewPager的监听
		mViewpager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				position = position % mUrls.size();
				// 选中的效果
				// 还原的效果
				for (int i = 0; i < mUrls.size(); i++) {
					ImageView iv = (ImageView) mContainerIndicator
							.getChildAt(i);
					iv.setImageResource(R.drawable.indicator_normal);
				}
				// 选中应该选中的
				ImageView iv = (ImageView) mContainerIndicator
						.getChildAt(position);
				iv.setImageResource(R.drawable.indicator_selected);
			}

			@Override
			public void onPageScrolled(int position, float positionOffset,
					int positionOffsetPixels) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub

			}
		});

		// 设置当前的选中索引
		// 设置当前选中的diff
		int diff = Integer.MAX_VALUE / 2 % mUrls.size();
		mViewpager.setCurrentItem(Integer.MAX_VALUE / 2 - diff);

		// 自动轮播的方法
		final AutoScrollTask autoScrollTask = new AutoScrollTask();
		autoScrollTask.start();

		// 按下去的时候停止轮播
		mViewpager.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					//按下的时候停止轮播
					autoScrollTask.stop();
					break;
				case MotionEvent.ACTION_MOVE:

					break;
				case MotionEvent.ACTION_UP:

					break;
				case MotionEvent.ACTION_CANCEL:
					//抬起手的时候继续轮播
					autoScrollTask.start();
					break;

				default:
					break;
				}
				return false;
			}
		});
	}

	/**
	 * 自动轮播的方法
	 * 
	 * @author Anson-pc
	 * 
	 */
	class AutoScrollTask implements Runnable {

		/**
		 * 开始轮播
		 */
		public void start() {
			UIUtils.getMainThreadHandler().postDelayed(this, 2000);
		}

		/**
		 * 结束轮播
		 */
		public void stop() {
			UIUtils.getMainThreadHandler().removeCallbacks(this);
		}

		@Override
		public void run() {
			int currentItem = mViewpager.getCurrentItem();
			currentItem++;
			mViewpager.setCurrentItem(currentItem);

			start();
		}

	}

	class PicturesAdapter extends PagerAdapter {

		@Override
		public int getCount() {
			if (mUrls != null) {
				return Integer.MAX_VALUE;
			}
			return 0;
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			// TODO Auto-generated method stub
			return view == object;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			position = position % mUrls.size();
			ImageView iv = new ImageView(UIUtils.getContext());
			iv.setScaleType(ScaleType.FIT_XY);
			// 图片的加载
			BitmapHepler.display(iv, URLS.IMAGEBASEURL + mUrls.get(position));
			container.addView(iv);
			return iv;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
	}
}
